<?php

namespace App\Tools\Math;
/**
 * Application level View Helper
 *
 * This file is application-wide helper file. You can put all
 * application-wide helper-related methods here.
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.View.Helper
 * @since         CakePHP(tm) v 0.2.9
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */

//App::uses('Helper', 'View');

/**
 * Application helper
 *
 * Add your application-wide methods in the class below, your helpers
 * will inherit them.
 *
 * @package       app.View.Helper
 */
class CmdHelper
{
    public static function numToCny($num)
    {
        $capUnit = array('万', '亿', '万', '圆', '');
        $capDigit = array(2 => array('角', '分', ''), 4 => array('仟', '佰', '拾', ''));
        $capNum = array('零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖');
        if ((strpos(strval($num), '.') > 16) || (!is_numeric($num)))
            return '';
        $num = sprintf("%019.2f", $num);
        $CurChr = array('', '');
        for ($i = 0, $ret = '', $j = 0; $i < 5; $i++, $j = $i * 4 + floor($i / 4)) {
            $nodeNum = substr($num, $j, 4);
            for ($k = 0, $subret = '', $len = strlen($nodeNum); (($k < $len) && (intval(substr($nodeNum, $k)) != 0)); $k++) {
                $CurChr[$k % 2] = $capNum[$nodeNum{$k}] . (($nodeNum{$k} == 0) ? '' : $capDigit[$len][$k]);
                if (!(($CurChr[0] == $CurChr[1]) && ($CurChr[$k % 2] == $capNum[0])))
                    if (!(($CurChr[$k % 2] == $capNum[0]) && ($subret == '') && ($ret == '')))
                        $subret .= $CurChr[$k % 2];
            }
            $subChr = $subret . (($subret == '') ? '' : $capUnit[$i]);
            if (!(($subChr == $capNum[0]) && ($ret == '')))
                $ret .= $subChr;
        }
        $ret = ($ret == "") ? $capNum[0] . $capUnit[3] : $ret;
        return $ret;
    }

    public static function numToCnyUnUnit($num)
    {
        $capUnit = array('万', '亿', '万', '圆', '');
        $capDigit = array(2 => array('角', '分', ''), 4 => array('仟', '佰', '拾', ''));
        $capNum = array('零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖');
        if ((strpos(strval($num), '.') > 16) || (!is_numeric($num)))
            return '';
        $num = sprintf("%019.2f", $num);
        $CurChr = array('', '');
        for ($i = 0, $ret = '', $j = 0; $i < 5; $i++, $j = $i * 4 + floor($i / 4)) {
            $nodeNum = substr($num, $j, 4);
            for ($k = 0, $subret = '', $len = strlen($nodeNum); (($k < $len) && (intval(substr($nodeNum, $k)) != 0)); $k++) {
                $CurChr[$k % 2] = $capNum[$nodeNum{$k}] . (($nodeNum{$k} == 0) ? '' : $capDigit[$len][$k]);
                if (!(($CurChr[0] == $CurChr[1]) && ($CurChr[$k % 2] == $capNum[0])))
                    if (!(($CurChr[$k % 2] == $capNum[0]) && ($subret == '') && ($ret == '')))
                        $subret .= $CurChr[$k % 2];
            }
            $subChr = $subret . (($subret == '') ? '' : $capUnit[$i]);
            if (!(($subChr == $capNum[0]) && ($ret == '')))
                $ret .= $subChr;
        }
        $ret = ($ret == "") ? $capNum[0] : $ret;
        $ret = str_replace('圆', '', $ret);
        return $ret;
    }

}
